package com.wpw.web.base;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @ClassName: WeakReferenceTest
 * @CreateDate: 2020-06-13 16:33
 * @author: wupengwei
 */
public class WeakReferenceTest {

    public static void main(String[] args) throws InterruptedException {
        /*CaiLiao liao = new CaiLiao();
        liao.setName("cailiao");
        SuLiaoDai dai = new SuLiaoDai(liao, 11);
        System.out.println(dai.getName());

        for (int i = 0; i <= 100; i++) {
            Byte[] bytes = new Byte[1024 * 102];
        }


        Thread.sleep(1000);

        System.out.println(dai.getName());*/

        ThreadLocal<String> string = new ThreadLocal<>();
        string.set("2");
        for (int i = 0; i <= 100; i++) {
            Byte[] bytes = new Byte[1024 * 102];
        }
        String s = string.get();
        System.out.println(s);

    }


}

class CaiLiao {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

class SuLiaoDai extends WeakReference<CaiLiao> {

    //Map<CaiLiao, Integer> map = new HashMap<>();

    Integer name;



    public SuLiaoDai(CaiLiao referent, Integer name) {
        super(referent);
        //map.put(referent, name);
//        Map<CaiLiao, Integer> map = new HashMap<>();
//        map.put(referent, name);
        this.name = name;
    }

    public Integer getName() {
        return name;
    }

    public void setName(Integer name) {
        this.name = name;
    }


}